import React, { useState, useMemo, useEffect } from 'react';
import { uJob, cline, obj_str, obj_int, ColView, filter_list, useHigh, GridTable, filter_row, useCmd, WaitUmd, MainView, BtTxt,
    Title, InfoDlg, show_mt, PanelFind, Btn, is_empty, loc } from 'sui';
import { fix_col, get_data } from 'test/base/cmd';
import { load_col, load_supply } from 'stock/base/cmd';
import { useAccess } from 'cache';
import { DlgNum } from './ctl';
import { show_val } from 'buy/task/cmd';

const SimpleView = ({ mod, submod, title, acc }) => {
    const [open_val, openVal] = useState(false);
    const [open_info, openInfo] = useState(false);
    const [map_supply, mapSupply] = useState();
    const [col, setCol] = useState();
    const [high] = useHigh(140, 100);
    const [no_acc] = useAccess();
    const umd = useCmd(`file/list/${mod}`, null, null, "tech");

    useEffect(() => {
        load_col(mod, setCol);
        load_supply(submod, null, mapSupply);
    }, [mod, submod]);

    const getRow = useMemo(() => {
        const lst = filter_list(["sn"], col);
        return filter_row(umd.value, lst, umd.filter);
    }, [umd.value, umd.filter, col]);

    const hreturn = () => {
        load_col(mod, setCol);
        umd.hreturn();
    };

    if (umd.isFin("col")) {
        const list = [
            {sn: mod, name: title, fix: fix_col()},
        ];

        return <ColView hreturn={hreturn} title={title} subtitle={loc("l_221")} list={list} cur={mod}/>;
    }

    const pact = {
        "fin": () => openVal(true),
        "back": () => openVal(true),
    };

    const act = {
        "fin": (v) => {
            umd.startWait();

            const cmd = {
                line: cline(),
                tp: submod,
                sn: obj_str(umd.cur, "sn"),
                num: obj_int(v, "num"),
                box: obj_int(v, "box"),
            };

            uJob(null, "stock/accept", cmd, r => umd.reload(umd.endWait), e => umd.setErr());
        },
        "back": (v) => {
            umd.startWait();

            const cmd = {
                line: cline(),
                tp: submod,
                sn: obj_str(umd.cur, "sn"),
                num: obj_int(v, "num"),
                box: obj_int(v, "box"),
            };

            uJob(null, "stock/back", cmd, r => umd.reload(umd.endWait), e => umd.setErr());
        },
    };

    const getCol = () => {
        if (is_empty(col)) {
            return null;
        }

        const tmp = [
            {name: loc("l_193"), key: 'index', width: 70, frozen: true},
            {name: loc("l_238"), key: 'e_opt', width: 130, frozen: true, formatter: props => {
                const d = props.row;
                return (
                    <div className="inline-flex">
                        <BtTxt hcmd={() => umd.pact(pact, d, "fin")} disabled={no_acc(acc)}>{loc("l_105")}</BtTxt>
                        <BtTxt hcmd={() => umd.pact(pact, d, "back")} disabled={no_acc(acc)}>{loc("l_111")}</BtTxt>
                    </div>
                );
            }},
        ];

        col.forEach(d => {
            if (d.sn === "supply") {
                tmp.push({name: d.name, key: d.sn, width: 150, formatter: props => show_val(props, "supply", map_supply)});
            } else {
                show_mt(tmp, d, 150);
            }
        });

        return tmp;
    };

    const Right = () => (
        <div className="inline-flex">
            <Btn className="mr-4" hcmd={() => umd.pact(null, null, "col")} disabled={no_acc(acc)}>{loc("l_221")}</Btn>
        </div>
    );

    const Left = () => <Title mod={title} sub={loc("l_778")}/>;

    return (
        <MainView>
            <PanelFind left={<Left/>} right={<Right/>} hcmd={v => umd.setFilter(v)}/>
            <GridTable col={getCol()} row={getRow} high={high}/>
            <DlgNum open={open_val} hcmd={v => umd.act(act, v)} setOpen={openVal} data={get_data(umd, map_supply)} mod={submod} col={col} fin={umd.fin}/>
            <InfoDlg open={open_info} setOpen={openInfo} msg={loc("l_882")}/>
            <WaitUmd umd={umd}/>
        </MainView>
    );
};

export {SimpleView};
